package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.helper.LoginHelper;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.WithdrawRecordBo;
import com.joker.orange.domain.vo.WithdrawRecordVo;
import com.joker.orange.domain.WithdrawRecord;
import com.joker.orange.mapper.WithdrawRecordMapper;
import com.joker.orange.service.IWithdrawRecordService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 提现管理Service业务层处理
 *
 * @author Joker
 * @date 2024-07-31
 */
@RequiredArgsConstructor
@Service
public class WithdrawRecordServiceImpl implements IWithdrawRecordService {

    private final WithdrawRecordMapper baseMapper;

    /**
     * 查询提现管理
     */
    @Override
    public WithdrawRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询提现管理列表
     */
    @Override
    public TableDataInfo<WithdrawRecordVo> queryPageList(WithdrawRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WithdrawRecord> lqw = buildQueryWrapper(bo);
        Page<WithdrawRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询提现管理列表
     */
    @Override
    public List<WithdrawRecordVo> queryList(WithdrawRecordBo bo) {
        LambdaQueryWrapper<WithdrawRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WithdrawRecord> buildQueryWrapper(WithdrawRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WithdrawRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, WithdrawRecord::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), WithdrawRecord::getUserName, bo.getUserName());
        lqw.eq(bo.getWithdrawMoney() != null, WithdrawRecord::getWithdrawMoney, bo.getWithdrawMoney());
        lqw.eq(StringUtils.isNotBlank(bo.getWithdrawType()), WithdrawRecord::getWithdrawType, bo.getWithdrawType());
        lqw.eq(StringUtils.isNotBlank(bo.getWithdrawCode()), WithdrawRecord::getWithdrawCode, bo.getWithdrawCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPlayMoneyType()), WithdrawRecord::getPlayMoneyType, bo.getPlayMoneyType());
        lqw.eq(StringUtils.isNotBlank(bo.getFailMsg()), WithdrawRecord::getFailMsg, bo.getFailMsg());
        lqw.eq(bo.getPlayMoneyTime() != null, WithdrawRecord::getPlayMoneyTime, bo.getPlayMoneyTime());
        lqw.eq(bo.getFailTime() != null, WithdrawRecord::getFailTime, bo.getFailTime());
        lqw.eq(bo.getWithdrawCount() != null, WithdrawRecord::getWithdrawCount, bo.getWithdrawCount());
        return lqw;
    }

    /**
     * 新增提现管理
     */
    @Override
    public Boolean insertByBo(WithdrawRecordBo bo) {
        WithdrawRecord add = BeanUtil.toBean(bo, WithdrawRecord.class);
        add.setUserId(LoginHelper.getUserId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            // 更新提现次数
            List<WithdrawRecord> list = baseMapper.selectList(new LambdaQueryWrapper<WithdrawRecord>()
                .eq(WithdrawRecord::getUserId, LoginHelper.getUserId()));
            WithdrawRecord update = new WithdrawRecord();
            update.setId(add.getId());
            update.setWithdrawCount(list.size());
            baseMapper.updateById(update);
        }
        return flag;
    }

    /**
     * 修改提现管理
     */
    @Override
    public Boolean updateByBo(WithdrawRecordBo bo) {
        WithdrawRecord update = BeanUtil.toBean(bo, WithdrawRecord.class);
        // 是否打款（0-已打款、1-未打款、2-拒绝打款）
        if ("2".equals(update.getPlayMoneyType())) {
            update.setFailTime(new Date());
        } else if ("0".equals(update.getPlayMoneyType())) {
            update.setPlayMoneyTime(new Date());
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WithdrawRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除提现管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
